import axios from "axios"

export const generate = () => {
  return new Promise(async(resolve, reject) => {
    try {
      const url = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation'
      const response = await axios({
        url,
        method: 'post',
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Bearer sk-a6b234744b8841b8ba61fb3dcd9fa8bd'
        },
        data: {
          model: 'qwen-turbo',
          input: {
            messages: [
              {
                role: "system",
                content: "You are a helpful assistant."
              },
              {
                role: "user",
                content: "请写一段自我介绍，10字以内"
              }
            ]
          }
        }
      })
      if (response.data.output.finish_reason === 'stop') {
        console.log(response.data.output.text, 'res');
        const article = response.data.output.text
        resolve(article)
      } else {
        reject('生成错误')
      }
    } catch (error) {
      reject(error)

    }
  })
}
